using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace ProjectCodeS.Models
{ 
    public class TicketRepository : ITicketRepository
    {
        ProjectCodeSContext context = new ProjectCodeSContext();

        public IQueryable<Ticket> All
        {
            get { return context.Tickets; }
        }

        public IQueryable<Ticket> AllIncluding(params Expression<Func<Ticket, object>>[] includeProperties)
        {
            IQueryable<Ticket> query = context.Tickets;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Ticket Find(int id)
        {
            return context.Tickets.Find(id);
        }

        public void InsertOrUpdate(Ticket ticket)
        {
            if (ticket.Id == default(int)) {
                // New entity
                context.Tickets.Add(ticket);
            } else {
                // Existing entity
                context.Entry(ticket).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var ticket = context.Tickets.Find(id);
            context.Tickets.Remove(ticket);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface ITicketRepository : IDisposable
    {
        IQueryable<Ticket> All { get; }
        IQueryable<Ticket> AllIncluding(params Expression<Func<Ticket, object>>[] includeProperties);
        Ticket Find(int id);
        void InsertOrUpdate(Ticket ticket);
        void Delete(int id);
        void Save();
    }
}